<template>
  <div>
    <el-table
        :data="tableData"
        border
        style="width: 100%">
      <el-table-column
          fixed
          prop="id"
          label="UserId"
          width="80">
      </el-table-column>
      <el-table-column
          prop="password"
          label="Password"
          width="750">
      </el-table-column>

      <el-table-column
          label="操作"
          width="100">
        <template slot-scope="scope">
          <el-button @click="edit(scope.row)" type="text" size="small">查看或修改</el-button>
          <!--          <el-button @click="deletepassword (scope.row)" type="text" size="small">删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>
    <!--实现分页查询-->
    <el-pagination
        background
        layout="prev, pager, next"
        page-size="6"
        :total=counts
        @current-change="page">
    </el-pagination>
  </div>

</template>

<script>
import JSEncrypt from 'jsencrypt/bin/jsencrypt';
//这里我不知道vue怎么写，但是这个publicKey和privateKey只用，而且只能生成一次，后面的时候都是使用这个key，所以要作为这个页面的变量，具体这个代码放哪我不清楚
let encryptor = new JSEncrypt();
//生成一对keyPair
let keyPair = encryptor.getKey();
//获得公钥和私钥，这里只有通过这种方法生成的密钥才是和服务器的算法一致的
let publicKey = keyPair.getPublicBaseKeyB64();
let privateKey = keyPair.getPrivateBaseKeyB64();
export default {
  methods: {
    //分页
    page(currentPage) {
      //alert(currentPage)
      const _this = this;
      //此处需要查询item的全部信息
      axios({
        method: 'GET',
        url: ' http://115.29.176.82:8081/manager/api/account',
        params: {
          size: 6,
          page: currentPage,
          publicKey: publicKey//查询的时候带上这个参数，作为服务器加密使用的
        }
      }).then(function (resp) {

        //这里设置私钥，以便后面解密
        encryptor.setPrivateKey(privateKey);
        //拿到数据后进行解密，对每一个user进行遍历，把密码进行解密
        resp.data.data.list.forEach(node => node.password = encryptor.decrypt(node.password));
        _this.tableData = (resp.data.data.list);
      });
    },

    edit(row) {
      this.$router.push({
        path: '/passwordupdate',
        query: {
          id: row.id,
          password: row.password,
        }
      });

    },


  },

  data() {
    return {
      counts:null,
      tableData: [{
        id: 'hhhh',
        password: '111111'
      }, {
        id: 'hhhh',
        password: '111111'
      }, {
        id: 'hhhh',
        password: '111111'
      }]
    };
  },
  created() {
    const _this = this;

    //获取数据
    axios({
      method: 'GET',
      url: ' http://115.29.176.82:8081/manager/api/account',
      params: {
        size: 6,
        page: 1,
        publicKey: publicKey
      }
    }).then(function (resp) {
      _this.counts=resp.data.data.counts
      resp.data.data.list.forEach(node => {
        encryptor.setPrivateKey(privateKey);
        node.password = encryptor.decrypt(node.password);
      });
      _this.tableData = (resp.data.data.list);

    });
  }
};
</script>